# !/usr/bin/env python
# -*- coding: utf-8 -*-
# @File  : agent_pool.py
# @Author: dongguangwen
# @Date  : 2024-06-29 14:11
import requests
import time
from lxml import etree
import random


class ProxyPool:
    # 初始化代理池
    def __init__(self):
        # 从代理网站上获取代理 IP
        self.proxy_urls = [
            'http://www.zdaye.com/free/',
            'http://www.zdaye.com/free/2'
            'http://www.zdaye.com/free/3'
            'http://www.zdaye.com/free/4'
        ]
        self.proxies = self.get_proxies()

    # 获取代理 IP
    def get_proxies(self):
        proxies = []
        for url in self.proxy_urls:
            headers = {
                'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'}
            response = requests.get(url, headers=headers)
            html = etree.HTML(response.text)
            ips = html.xpath('//*[@id="ipc"]/tbody/tr/td[1]/text()')
            ports = html.xpath('//*[@id="ipc"]/tbody/tr/td[2]/text()')
            for i in range(len(ips)):
                proxies.append('http://' + ips[i] + ':' + ports[i])
        return self.check_proxies(proxies)

    # 检查代理 IP 的可用性
    def check_proxies(self, proxies):
        valid_proxies = []
        for proxy in proxies:
            try:
                requests.get('https://www.baidu.com', proxies={'http': proxy, 'https': proxy}, timeout=3)
                print(proxy)
                valid_proxies.append(proxy)
                time.sleep(1)
            except:
                continue
        return valid_proxies

    # 随机获取代理 IP
    def get_proxy(self):
        proxy = random.choice(self.proxies)
        return proxy


if __name__ == '__main__':
    proxy = ProxyPool()
    res = proxy.get_proxy()
    print(res)
